/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.recovery.appweb;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.utils.UUID;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.utils.KdniaoTrackQueryAPI;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.recovery.entity.ZtRecovery;
import com.jeesite.modules.recovery.service.ZtRecoveryService;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;

/**
 * 回购Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztRecovery")
public class AppZtRecoveryController extends BaseController {

	@Autowired
	private ZtRecoveryService ztRecoveryService;
	
	@Autowired
	private GoodsTypeService goodsTypeService;
	
	/**
	 * 查询列表数据
	 */
	
	@RequestMapping(value = "listData")
	@ResponseBody
	public void listData(HttpServletRequest request,
			HttpServletResponse response,Model model) {	
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String adminid = jo.getString("adminid");
		
		ZtRecovery ztRecovery=new ZtRecovery();	
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztRecovery.setZtAdmin(ztAdmin);
		List<ZtRecovery> page = ztRecoveryService.findList(ztRecovery);	
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(page);
		Render.renderString(response, res);
		
	}
	
	/**
	 * 查看编辑表单
	 */
	
	@RequestMapping(value = "form")
	public void form(HttpServletRequest request,HttpServletResponse response) {
	
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String recoveryid = jo.getString("recoveryid");
		
		ZtRecovery ztRecovery=new ZtRecovery();	
		ztRecovery.setId(recoveryid);
		ztRecovery = ztRecoveryService.get(ztRecovery);	
		
		
		if (StringUtils.isNotBlank(ztRecovery.getCodestatus())
				&& !"4".equals(ztRecovery.getCodestatus())) {
			String logistics = ztRecovery.getLogistics();
			String logisticsCode = ztRecovery.getLogisticsCode();
			
			if (StringUtils.isNotBlank(logistics)
					&& StringUtils.isNotBlank(logisticsCode)) {
				try {
					KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
					String result = api.getOrderTracesByJson(logisticsCode,
							logistics);

					
					JSONObject job = JSONObject.parseObject(result);
					String state = job.getString("State");
					if ("3".equals(state)) {
						ztRecovery.setCodestatus("4");
						ztRecoveryService.save(ztRecovery);
					}
				} catch (Exception ex) {
					ex.printStackTrace();

				}
			}
		}
		
		ResultObject res = new ResultObject();
		res.setResult(200L);
		res.setData(ztRecovery);
		Render.renderString(response, res);
	}
	

	/**
	 * 保存数据
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public void save(HttpServletRequest request,HttpServletResponse response) {
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String isuse = jo.getString("isuse");
		String iscomplete = jo.getString("iscomplete");
		String iswear = jo.getString("iswear");
		String remarks = jo.getString("remarks");
		String code = String.valueOf(UUID.next());
		String title = jo.getString("title");
		String price = jo.getString("price");
		String goodsType = jo.getString("goodsType");
		String model = jo.getString("model");
		String contacts = jo.getString("contacts");
		String phone = jo.getString("phone");
		String name = jo.getString("name");
		String ident = jo.getString("ident");
		String imgs = jo.getString("imgs");
		String adminid = jo.getString("adminid");
		
		String license = jo.getString("license");
		String froIdcard = jo.getString("froIdcard");
		String backIdcard = jo.getString("backIdcard");
		
		
		ZtRecovery ztRecovery=new ZtRecovery();	
		
		ztRecovery.setLicense(license);
		ztRecovery.setFroIdcard(froIdcard);
		ztRecovery.setBackIdcard(backIdcard);
		
		ztRecovery.setIsuse(isuse);
		ztRecovery.setIscomplete(iscomplete);
		ztRecovery.setIswear(iswear);
		ztRecovery.setRemarks(remarks);
		ztRecovery.setCode(code);
		ztRecovery.setTitle(title);
		ztRecovery.setPrice(Double.valueOf(price));
		ztRecovery.setModel(model);
		ztRecovery.setContacts(contacts);
		ztRecovery.setPhone(phone);
		ztRecovery.setName(name);
		ztRecovery.setIdent(ident);
		ztRecovery.setImgs(imgs);
		ztRecovery.setCreateDate(new Date());
		
		GoodsType gt=new GoodsType(); 
		gt.setId(goodsType);
		ztRecovery.setGoodsType(gt);
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztRecovery.setZtAdmin(ztAdmin);
		
		ztRecovery.setCodestatus("1");
		
		ztRecoveryService.save(ztRecovery);
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(ztRecovery);
		Render.renderString(response, res);
		
	}
	

	/**
	 * 保存物流信息
	 */
	@PostMapping(value = "saveLogistics")
	@ResponseBody
	public void saveLogistics(HttpServletRequest request,HttpServletResponse response) {
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String recoveryid = jo.getString("recoveryid");
		String logistics = jo.getString("logistics");
		String logisticsName = jo.getString("logisticsName");
		
		ZtRecovery ztRecovery=new ZtRecovery();	
		ztRecovery.setId(recoveryid);
		ztRecovery = ztRecoveryService.get(ztRecovery);	
		
		ztRecovery.setLogistics(logistics);
		ztRecovery.setLogisticsName(logisticsName);
		ztRecovery.setCodestatus("3");
		ztRecoveryService.save(ztRecovery);
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(ztRecovery);
		Render.renderString(response, res);
	}
	
}